package com.mantru.iotpot.modbus;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Message;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import com.mantru.iotpot.dao.*;
import com.mantru.iotpot.*;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import static android.content.Context.TELEPHONY_SERVICE;

/**
 * WebView浏览器辅助工具类
 * 作者： cmh
 * 日期： 2021/12/31
 */

public class WebUtil
{
    //初始化webview浏览器，主要是开启js、自适应屏幕等
    public static void InitWebview(WebView web)
    {
        WebSettings web_settings = web.getSettings();
        //这里必须开启，打开JavaScript调用功能，js调用安卓api
        web_settings.setJavaScriptEnabled(true);
        //设置DOM缓存，当H5网页使用localStorage时，一定要设置
        web_settings.setDomStorageEnabled(true);
        // 支持缩放(适配到当前屏幕)
        web_settings.setSupportZoom(true);
        //设置加载进来的页面自适应手机屏幕的分辨率
        web_settings.setUseWideViewPort(true);
        web_settings.setLoadWithOverviewMode(true);
        //开启webview使用文件协议权限
        web_settings.setAllowFileAccess(true);
        // 支持内容重新布局,一共有四种方式
        // 默认的是NARROW_COLUMNS
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            web_settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
        } else {
            web_settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        }
        // 设置可以被显示的屏幕控制
        web_settings.setDisplayZoomControls(true);
        //禁止长按事件，不允许选择文本
        web.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        web.setLongClickable(false);
    }

    /*  js调用安卓，必须加@JavascriptInterface注释的方法才可以被js调用  */
    //读取系统固定的数据，如：user_id、token、screen
    public static String GetAppData(ApplicationClass ac, Context context, String key_name)
    {
        String key_value = "";
        try
        {
            //读取屏幕分辨率
            if (key_name.toLowerCase().equals("screen"))
            {
                DisplayMetrics dm = context.getResources().getDisplayMetrics();
                int width = dm.widthPixels;
                int height = dm.heightPixels;
                key_value = "{\"width\":" + width + ",\"height\":" + height + "}";
            }
            else if (key_name.toLowerCase().equals("app_version"))
            {
                key_value = ac.APP_Version;
            }
            else if (key_name.toLowerCase().equals("net_work_status"))
            {
                key_value = String.valueOf(ac.net_work_status);
            }
            else if (key_name.toLowerCase().equals("init_data_progress"))
            {
                key_value = String.valueOf(ac.init_data_progress);
            }
            else if (key_name.toLowerCase().equals("error_info"))
            {
                //2023.08.11 将0012换成E12的格式
                if (ac.error_code.startsWith("00"))
                    ac.error_code = "E" + ac.error_code.substring(2).toUpperCase();
                key_value = "{\"success\":true,\"code\":\"" + ac.error_code + "\",\"desc\":\"" + ac.error_desc + "\"}";
            }
            else if (key_name.toLowerCase().equals("qrcode"))
            {
                key_value = "{\"success\":true,\"device_id\":\"" + ac.activity_device_id + "\",\"url\":\"" + Common.API_Server_Url + "?txt_type=/pot/device_qrcode/&device_id="+ac.activity_device_id+"\"}";
            }
            else if (key_name.toLowerCase().equals("page_index"))
            {
                ac.page_index_last_time = Common.GetTimeStamp13();
                key_value = "1";
            }
            else
            {
                //其它参数直接从缓存中读取
                SharedPreferences sp = context.getSharedPreferences("cache_data", Context.MODE_PRIVATE);
                key_value = sp.getString(key_name, "");
            }
        }
        catch(Exception ex)
        {}
        return key_value;
    }

    //从缓存中读取数据
    public static String GetCacheData(Context context, String key_name)
    {
        String key_value = "";
        try
        {
            SharedPreferences sp = context.getSharedPreferences("cache_data", Context.MODE_PRIVATE);
            key_value = sp.getString(key_name, "");
        }
        catch(Exception ex)
        {}
        return key_value;
    }

    //写入数据到缓存中
    public static int SaveCacheData(Context context, String key_name, String key_value)
    {
        try
        {
            SharedPreferences sp = context.getSharedPreferences("cache_data", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(key_name, key_value);
            editor.commit();
        }
        catch(Exception ex)
        {}
        return 0;
    }

    //发送设备复位指令
    public static int Device_ResetStatus(ApplicationClass ac)
    {
        try
        {
            ac.com.writeSingleRegister(0x01, 0x0012, 0x6A, null);
        }
        catch(Exception ex)
        {}
        return 1;
    }

//    //打开新窗口
//    public static int Open(Context context, String url)
//    {
//        try
//        {
//            url = Common.FormatWebUrl(url);
//            Intent start_intent = new Intent(context.getApplicationContext(), ActivityWeb.class);
//            start_intent.putExtra("open_url", url);
//            context.startActivity(start_intent);
//        }
//        catch(Exception ex)
//        {}
//        return 0;
//    }

    //关闭窗口，back_off_index=-1表示关闭当前窗口（后退1个窗口），-2表示后退2个窗口...，0表示关闭全部弹窗直至首页
    public static int Close(ApplicationClass ac, int back_off_index)
    {
        int index = back_off_index;
        if (index > 0)
            index = index * -1;

        return 0;
    }

    //叫起安卓的网络设置界面（设置wifi账号、密码）
    public static int OpenNetworkConfig(Context context)
    {
        try
        {
            context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));   //直接进入手机中的wifi网络设置界面
            //context.startActivity(new Intent(Settings.ACTION_SETTINGS));      //直接进入手机中设置界面
        }
        catch(Exception ex)
        {}
        return 0;
    }

    //读取系统菜谱分类
    public static String GetSQLData_Vegetable_Type(ApplicationClass ac)
    {
        try
        {
            t_vegetable_info_type ent = new t_vegetable_info_type();
            Cursor dt = ac.dao.GetCursor(ent.list_sql, null);
            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = "{\"success\":true,\"desc\":\"成功\",\"item_count\":" + item_count + ",\"item_data\":[" + item_data + "]}";
            dt.close();
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //保存系统菜谱分类
    public static String SaveSQLData_Vegetable_Type(ApplicationClass ac, String type_id, String type_name)
    {
        try
        {
            t_vegetable_info_type ent = new t_vegetable_info_type();
            ent.type_id = type_id;
            ent.type_name = type_name;
            //编辑
            if (type_id.equals("0") == false){
                ac.dao.ExecSQL(ent.update_sql, ent.GetUpdateValues());
                String return_data = "{\"success\":true,\"desc\":\"保存成功\"}";
                return return_data;
            }
            //新增
            Cursor dt = ac.dao.GetCursor(ent.max_sql, null);
            int item_count = dt.getCount();
            String max_data = ac.dao.Cursor2Json(dt);
            dt.close();
            if (item_count > 0) {
                int int_type_id = Integer.parseInt(Common.JSON_GetFieldValue(max_data, "type_id"));
                int int_sort_index = Integer.parseInt(Common.JSON_GetFieldValue(max_data, "sort_index"));
                ent.type_id = String.valueOf(int_type_id + 1);
                ent.sort_index = String.valueOf(int_sort_index + 1);
                ent.status = "1";
                ent.parent_type_id = "0";
                ac.dao.Insert(ent.table_name, ent.GetInsertContentValues());
                String return_data = "{\"success\":true,\"desc\":\"保存成功\"}";
                return return_data;
            }
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"保存失败\"}";
    }

    //删除系统菜谱分类
    public static String DeleteSQLData_Vegetable_Type(ApplicationClass ac, String type_id)
    {
        try
        {
            String[] id_list = type_id.split(",");
            for (int i=0; i<id_list.length; i++) {
                t_vegetable_info_type ent = new t_vegetable_info_type();
                ent.type_id = id_list[i];
                //删除
                if (ent.type_id.equals("0") == false)
                    ac.dao.ExecSQL(ent.delete_sql, ent.GetDeleteValues());
            }
            String return_data = "{\"success\":true,\"desc\":\"删除成功\"}";
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"删除失败\"}";
    }

    //读取系统菜谱列表，save_type=1系统菜谱、2我的菜谱
    public static String GetSQLData_Vegetable_List(ApplicationClass ac, String save_type, String type_id)
    {
        try
        {
            if (type_id == null || type_id.trim().length() <= 0)
                type_id = "0";
            t_vegetable_info ent = new t_vegetable_info();
            //ent.save_type = save_type;
            //ent.type_id1 = type_id;
            Cursor dt = null;
            if (type_id.equals("0"))
                dt = ac.dao.GetCursor(ent.list_sql1, ent.GetList1Values());
            else
                dt = ac.dao.GetCursor(ent.list_sql2, ent.GetList2Values());
            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = "{\"success\":true,\"desc\":\"成功\",\"item_count\":" + item_count + ",\"item_data\":[" + item_data + "]}";
            dt.close();
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"失败\"}";
    }
    //读取系统菜谱列表，save_type=1系统菜谱、2我的菜谱
    public static String GetSQLData_Vegetable_List(ApplicationClass ac, int count)
    {
        try
        {
            t_vegetable_info ent = new t_vegetable_info();
            ent.company_id = ac.company_id;
            ent.parent_company_id = ac.parent_company_id;
            String sql = ent.list_sql31 + count;  //按照已经炒过的次数倒序
            if (ac.activity_vegetable_order_by.equals("1") == false)
                sql = ent.list_sql30 + count;     //按照最后一次炒制的时间倒序
            Cursor dt = ac.dao.GetCursor(sql, ent.GetList3Values());
            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = "{\"success\":true,\"desc\":\"成功\",\"item_count\":" + item_count + ",\"item_data\":[" + item_data + "]}";
            dt.close();
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //读取最近使用的词
    public static String GetSQLData_Vegetable_Search_GetText(ApplicationClass ac, int count)
    {
        try
        {
            t_search_text ent = new t_search_text();

            Cursor dt = ac.dao.GetCursor(ent.list_sql + " order by timestamp desc limit " + count, ent.GetListValues());
            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = "{\"success\":true,\"desc\":\"成功\",\"item_count\":" + item_count + ",\"item_data\":[" + item_data + "]}";
            dt.close();
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //读取基础食材列表
    public static String GetSQLData_Vegetable_List_Base(ApplicationClass ac, int count)
    {
        try
        {
            t_base_text ent = new t_base_text();
            Cursor dt = ac.dao.GetCursor(ent.list_sql + " order by base_name limit " + count, ent.GetListValues());
            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = "{\"success\":true,\"desc\":\"成功\",\"item_count\":" + item_count + ",\"item_data\":[" + item_data + "]}";
            dt.close();
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"失败\"}";
    }
    //保存基础食材
    public static void SaveSQLData_Vegetable_Base(ApplicationClass ac, String base_name)
    {
        try
        {
            t_base_text ent = new t_base_text();
            ent.base_name = base_name;
            ac.dao.ExecSQL(ent.delete_sql, ent.GetDeleteValues());
            ac.dao.Insert(ent.table_name, ent.GetInsertContentValues());

        }
        catch(Exception ex)
        {}
    }

    //搜索系统菜谱列表
    // search_type排序类型：1综合排序、2首字母排序、3本地研发的菜谱(新增和录制)
    // search_data内容
    // count 数量
    // search_char 首字母
    public static String GetSQLData_Vegetable_Search(ApplicationClass ac, int search_type, String search_data, int count, String search_char)
    {
        try
        {
            if (search_data == null) search_data = "";
            if (search_char == null) search_char = "";
            search_data = search_data.trim();
            search_data = search_data.replace("'", "");
            search_data = search_data.replace("\"", "");
            search_data = search_data.replace("%", "");

            //保存搜索词
            if (search_data.length() > 0) {
                t_search_text text1 = new t_search_text();
                text1.text_value = search_data;
                text1.timestamp = "" + Common.GetTimeStamp13();
                ac.dao.ExecSQL(text1.delete_sql, text1.GetDeleteValues());
                ac.dao.Insert(text1.table_name, text1.GetInsertContentValues());
            }

            t_vegetable_info ent = new t_vegetable_info();
            //ent.save_type = save_type;
            ent.company_id = ac.company_id;
            ent.parent_company_id = ac.parent_company_id;
            ent.search_data = search_data;
            Cursor dt = null;
            if (search_type == 1) {
                //1综合排序
                ent.search_sql1 = ent.search_sql1.replace("%search_data%", search_data) + count;
                dt = ac.dao.GetCursor(ent.search_sql1, ent.GetSearch1Values());
            }
            if (search_type == 2) {
                //2首字母排序
                String char_sql = "";
                if (search_char.length() > 0)
                    char_sql = " and firsh_char='" + search_char + "' ";
                ent.search_sql2 = ent.search_sql2.replace("%search_data%", search_data).replace("%search_char%", char_sql) + count;
                dt = ac.dao.GetCursor(ent.search_sql2, ent.GetSearch2Values());
            }
            if (search_type == 3) {
                //3本地研发的菜谱(新增和录制)
                ent.search_sql3 = ent.search_sql3.replace("%search_data%", search_data) + count;
                dt = ac.dao.GetCursor(ent.search_sql3, ent.GetSearch3Values());
            }
            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = "{\"success\":true,\"desc\":\"成功\",\"item_count\":" + item_count + ",\"item_data\":[" + item_data + "]}";
            dt.close();
            return return_data;
        }
        catch(Exception ex)
        {
            ac.SaveLog("GetSQLData_Vegetable_Search("+search_type+","+search_data+","+search_char+")出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //读取系统菜谱详情
    public static String GetSQLData_Vegetable_Entity(ApplicationClass ac, String vegetable_id)
    {
        try
        {
            //读取基本信息
            t_vegetable_info ent = new t_vegetable_info();
            ent.company_id = ac.company_id;
            ent.parent_company_id = ac.parent_company_id;
            ent.vegetable_id = vegetable_id;
            String[] params = new String[1];
            params[0] = vegetable_id;
            Cursor dt = ac.dao.GetCursor(ent.entity_sql, params);
            String base_data = ac.dao.Cursor2Json(dt);
            System.out.print("读出来vegetable_id="+vegetable_id+",data=" + base_data);
            String return_data = "{\"success\":true,\"desc\":\"成功\",\"data\":" + base_data + "}";
            dt.close();

            return return_data;
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_Vegetable_Entity("+vegetable_id+")出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //读取系统菜谱详情
    public static String GetSQLData_Vegetable_Entity_ByName(ApplicationClass ac, String vegetable_name)
    {
        try
        {
            //读取基本信息
            t_vegetable_info ent = new t_vegetable_info();
            ent.company_id = ac.company_id;
            ent.parent_company_id = ac.parent_company_id;
            ent.vegetable_name = vegetable_name;
            String[] params = new String[3];
            params[0] = ent.company_id;
            params[1] = ent.parent_company_id;
            params[2] = vegetable_name;
            Cursor dt = ac.dao.GetCursor(ent.entity_sql2, params);
            String base_data = ac.dao.Cursor2Json(dt);

            String return_data = "{\"success\":true,\"desc\":\"成功\",\"data\":" + base_data + "}";
            dt.close();

            return return_data;
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_Vegetable_Entity_ByName("+vegetable_name+")出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //读取系统菜谱详情--编辑界面用
    public static String GetSQLData_Vegetable_Entity_For_Edit(ApplicationClass ac, String vegetable_id)
    {
        try
        {
            String json_data = GetSQLData_Vegetable_Entity(ac, vegetable_id);
            //System.out.print("11------" + json_data);
            JSONObject json_object = new JSONObject(json_data);
            if (json_object.getBoolean("success")) {
                //遍历规格
                JSONArray spec_array = json_object.getJSONObject("data").getJSONArray("spec_data");
                for (int i=0; i<spec_array.length(); i++) {
                    //遍历步骤
                    int total_time = spec_array.getJSONObject(i).getInt("end_time");
                    JSONArray steep_array = spec_array.getJSONObject(i).getJSONArray("steep_data");
                    for (int j=1; j<steep_array.length(); j++) {

                        //时长
                        int time_long1 = steep_array.getJSONObject(j-1).getInt("time_long");
                        int time_long2 = steep_array.getJSONObject(j).getInt("time_long");
                        int time_long3 = time_long2 - time_long1;
                        if (time_long3 < 0)
                            time_long3 = 1;
                        //将时长设置为开始时间
                        steep_array.getJSONObject(j-1).put("time_long", time_long3);
                        steep_array.getJSONObject(j-1).put("time_value", Common.int2time(time_long3));
                        if (j == steep_array.length() - 1) {
                            time_long3 = total_time - time_long2;
                            if (time_long3 < 0)
                                time_long3 = 1;
                            steep_array.getJSONObject(j).put("time_long", time_long3);
                            steep_array.getJSONObject(j).put("time_value", Common.int2time(time_long3));
                        }

                        //步骤类型：1加热设置、2调味料投放、3料盒投放、4手动投放
                        int steep_type = steep_array.getJSONObject(j).getInt("steep_type");
                        if (steep_type == 3) {
                            if (steep_array.getJSONObject(j).has("box_data") == false) {
                                JSONArray box_data = new JSONArray();
                                JSONObject box_obj = new JSONObject();
                                int box1_value = steep_array.getJSONObject(j).getInt("box1_value");
                                int box2_value = steep_array.getJSONObject(j).getInt("box2_value");
                                int box3_value = steep_array.getJSONObject(j).getInt("box3_value");
                                int box4_value = steep_array.getJSONObject(j).getInt("box4_value");
                                if (box1_value > 0) {
                                    box_obj.put("name", steep_array.getJSONObject(j).getString("box1_name"));
                                    box_obj.put("value", box1_value);
                                }
                                else if (box2_value > 0) {
                                    box_obj.put("name", steep_array.getJSONObject(j).getString("box2_name"));
                                    box_obj.put("value", box2_value);
                                }
                                else if (box3_value > 0) {
                                    box_obj.put("name", steep_array.getJSONObject(j).getString("box3_name"));
                                    box_obj.put("value", box3_value);
                                }
                                else if (box4_value > 0) {
                                    box_obj.put("name", steep_array.getJSONObject(j).getString("box4_name"));
                                    box_obj.put("value", box4_value);
                                }
                                box_data.put(box_obj);
                                steep_array.getJSONObject(j).put("box_data", box_data);
                            }
                        }
                    }
                    if (steep_array.length() == 1) {
                        steep_array.getJSONObject(0).put("time_long", total_time);
                        steep_array.getJSONObject(0).put("time_value", Common.int2time(total_time));
                        //步骤类型：1加热设置、2调味料投放、3料盒投放、4手动投放
                        int steep_type = steep_array.getJSONObject(0).getInt("steep_type");
                        if (steep_type == 3) {
                            if (steep_array.getJSONObject(0).has("box_data") == false) {
                                JSONArray box_data = new JSONArray();
                                JSONObject box_obj = new JSONObject();
                                int box1_value = steep_array.getJSONObject(0).getInt("box1_value");
                                int box2_value = steep_array.getJSONObject(0).getInt("box2_value");
                                int box3_value = steep_array.getJSONObject(0).getInt("box3_value");
                                int box4_value = steep_array.getJSONObject(0).getInt("box4_value");
                                if (box1_value > 0) {
                                    box_obj.put("name", steep_array.getJSONObject(0).getString("box1_name"));
                                    box_obj.put("value", box1_value);
                                }
                                else if (box2_value > 0) {
                                    box_obj.put("name", steep_array.getJSONObject(0).getString("box2_name"));
                                    box_obj.put("value", box2_value);
                                }
                                else if (box3_value > 0) {
                                    box_obj.put("name", steep_array.getJSONObject(0).getString("box3_name"));
                                    box_obj.put("value", box3_value);
                                }
                                else if (box4_value > 0) {
                                    box_obj.put("name", steep_array.getJSONObject(0).getString("box4_name"));
                                    box_obj.put("value", box4_value);
                                }
                                box_data.put(box_obj);
                                steep_array.getJSONObject(0).put("box_data", box_data);
                            }
                        }
                    }
                }
            }
            //System.out.print("22------" + json_object.toString());
            return json_object.toString();
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_Vegetable_Entity_For_Edit("+vegetable_id+")出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //判断菜谱名称是否存在
    public static String GetSQLData_Vegetable_IsExists(ApplicationClass ac, String vegetable_id, String vegetable_name)
    {
        String is_exists = "1";
        try
        {
            t_vegetable_info ent = new t_vegetable_info();
            ent.company_id = ac.company_id;
            ent.parent_company_id = ac.parent_company_id;
            ent.vegetable_id = vegetable_id;
            ent.vegetable_name = vegetable_name;
            Cursor dt_exists;
            if (vegetable_id.equals("0"))
                dt_exists = ac.dao.GetCursor(ent.exists_sql1, ent.GetExistsValues1());
            else
                dt_exists = ac.dao.GetCursor(ent.exists_sql2, ent.GetExistsValues2());
            if (dt_exists.getCount() <= 0)
                is_exists = "0";
            dt_exists.close();
            String return_data = "{\"success\":true,\"is_exists\":" + is_exists+ "}";
            return return_data;
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_Vegetable_IsExists("+vegetable_id+","+vegetable_name+")出错：" + ex.getMessage());
        }
        return "{\"success\":true,\"is_exists\":1}";
    }

    //判断菜谱名称+份数是否存在
    //copies_qty份数，1-10
    public static String GetSQLData_VegetableAndSpec_IsExists(ApplicationClass ac, String vegetable_id, String vegetable_name, String copies_qty)
    {
        String is_exists = "1";
        try
        {
            t_vegetable_info ent = new t_vegetable_info();
            ent.company_id = ac.company_id;
            ent.parent_company_id = ac.parent_company_id;
            ent.vegetable_id = vegetable_id;
            ent.vegetable_name = vegetable_name;
            Cursor dt_exists;
            if (vegetable_id.equals("0"))
                dt_exists = ac.dao.GetCursor(ent.exists_sql1, ent.GetExistsValues1());
            else
                dt_exists = ac.dao.GetCursor(ent.exists_sql2, ent.GetExistsValues2());
            if (dt_exists.getCount() <= 0)
                is_exists = "0";

            String exists_vegetable_id = "0";
            if (is_exists.equals("1")) {
                String data = ac.dao.Cursor2Json(dt_exists);
                exists_vegetable_id = Common.JSON_GetFieldValue(data, "vegetable_id");
            }
            dt_exists.close();

            if (is_exists.equals("1")) {
                ac.SaveLog("exists_vegetable_id=" + exists_vegetable_id);
                String json_data = GetSQLData_Vegetable_Entity(ac, exists_vegetable_id);
                JSONObject json_object = new JSONObject(json_data);
                JSONObject data_object = json_object.getJSONObject("data");
                //读取全部规格
                JSONArray spec_data = data_object.getJSONArray("spec_data");
                JSONObject spec_object = null;
                for (int i=0; i<spec_data.length(); i++){
                    if (spec_data.getJSONObject(i).getString("copies_qty").equals(copies_qty)) {
                        spec_object = spec_data.getJSONObject(i);
                        break;
                    }
                }
                if (spec_object == null)
                    is_exists = "0";
            }

            String return_data = "{\"success\":true,\"is_exists\":" + is_exists+ ",\"exists_vegetable_id\":"+exists_vegetable_id+"}";
            return return_data;
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_VegetableAndSpec_IsExists("+vegetable_id+","+vegetable_name+","+copies_qty+")出错：" + ex.getMessage());
        }
        return "{\"success\":true,\"is_exists\":1,\"exists_vegetable_id\":0}";
    }


    //删除指定菜谱名称的记录
    public static void GetSQLData_Vegetable_Delete(ApplicationClass ac, String save_type, String vegetable_id, String vegetable_name)
    {
        try
        {
            t_vegetable_info ent = new t_vegetable_info();
            //ent.save_type = save_type;
            ent.company_id = ac.company_id;
            ent.parent_company_id = ac.parent_company_id;
            ent.vegetable_id = vegetable_id;
            ent.vegetable_name = vegetable_name;

            if (vegetable_id.equals("0"))
                ac.dao.ExecSQL(ent.delete_sql1, ent.GetExistsValues1());
            else
                ac.dao.ExecSQL(ent.delete_sql3, ent.GetDeleteValues3());
        }
        catch(Exception ex)
        {}
    }

    //保存菜谱，save_type=1系统菜谱、2我的菜谱，is_sync=true表示用多线程下载图片
    public static String SaveSQLData_Vegetable_Entity(ApplicationClass ac, String save_type, String vegetable_id, String base_data, String steep_data, String from_vegetable_id, boolean is_sync)
    {
        if (from_vegetable_id == null || from_vegetable_id.length() <= 0)
            from_vegetable_id = "0";
        if (vegetable_id == null || vegetable_id.length() <= 0){
            return "{\"success\":false,\"desc\":\"保存失败\",\"vegetable_id\":\""+vegetable_id+"\"}";
        }
        if (steep_data.length() <= 10)
            steep_data = "{\"steep_count\":0,\"steep_data\":[]}";
        String error = "", aaaa = "1111", bbbb = "";
        try
        {
            //主表更新时间
            t_vegetable_info ent_main = new t_vegetable_info();
            ent_main.vegetable_id = vegetable_id;
            ent_main.from_vegetable_id = from_vegetable_id;
            //ent_main.save_type = save_type;
            ent_main.update_time = String.valueOf(Common.GetTimeStamp10());
            ent_main.last_work_time =  String.valueOf(Common.GetTimeStamp10());
            //针对保存到我的菜谱要做过滤
            if (save_type.equals("2") && vegetable_id.equals("0") && from_vegetable_id.equals("0") == false) {
                boolean is_exists = false;
                Cursor dt_exists = ac.dao.GetCursor(ent_main.exists_sql3, ent_main.GetExistsValues3());
                if (dt_exists.getCount() > 0)
                    is_exists = true;
                dt_exists.close();
                if (is_exists) {
                    String return_data = "{\"success\":false,\"desc\":\"菜谱已存在\",\"from_vegetable_id\":\""+from_vegetable_id+"\"}";
                    return return_data;
                }
            }
            aaaa = "2222";
            if (base_data.length() > 10) {
                ent_main.vegetable_name = Common.JSON_GetFieldValue(base_data, "vegetable_name");
                ent_main.firsh_char = Common.JSON_GetFieldValue(base_data, "firsh_char");
                ent_main.face_image_url = Common.JSON_GetFieldValue(base_data, "image_type");
                if (ent_main.face_image_url.length() <= 0)
                    ent_main.face_image_url = Common.JSON_GetFieldValue(base_data, "face_image_url").replace("\\", "");
                if (ent_main.face_image_url.length() <= 0)
                    ent_main.face_image_url = "1";

                JSONArray json_array = new JSONArray(base_data);
                JSONObject json_object = (JSONObject)json_array.get(0);
                aaaa = "3333";
                ent_main.work_time = "00:00";
                ent_main.user_work_qty = "0";
                ent_main.sort_index = "0";
                aaaa = "4444";

                aaaa = "5555";
                //下载封面图
                bbbb += ",face_image_url=" + ent_main.face_image_url;
                String face_image_url = ent_main.face_image_url;
                if (face_image_url.startsWith("/")) {
                    face_image_url = Common.File_Server_Url + face_image_url;
                    String url_file_name1 = ent_main.face_image_url.substring(ent_main.face_image_url.lastIndexOf("/") + 1);
                    ent_main.face_image_url = Common.GetCacheFilesUrl(ac.main_context, url_file_name1);
                    bbbb += "+" + face_image_url + "+is_sync=" + is_sync;
                    //启动线程将图片文件下载到本地缓存
                    DownFileThread down = new DownFileThread(ac.main_context, face_image_url, null, is_sync);
                    down.start();
                }
                aaaa = "6666";
                //读以旧vegetable_id
                if (vegetable_id.equals("0") == true && from_vegetable_id.equals("0") == false )
                {
                    Cursor dt_main_ent = ac.dao.GetCursor(ent_main.entity_from_sql, ent_main.GetEntityFromValues());
                    int item_count = dt_main_ent.getCount();
                    String ent_data = ac.dao.Cursor2Json(dt_main_ent);
                    dt_main_ent.close();
                    if (item_count > 0) {
                        long id1 = Long.parseLong(Common.JSON_GetFieldValue(ent_data, "vegetable_id"));
                        long id2 = Long.parseLong(Common.JSON_GetFieldValue(ent_data, "sort_index"));
                        vegetable_id = String.valueOf(id1);
                        ent_main.sort_index = String.valueOf(id2);
                    }
                }
                aaaa = "666666666";
                //新增
                if (vegetable_id.equals("0")){
                    Cursor dt_main_max = ac.dao.GetCursor(ent_main.max_sql, null);
                    int item_count = dt_main_max.getCount();
                    String max_data = ac.dao.Cursor2Json(dt_main_max);
                    dt_main_max.close();
                    if (item_count <= 0) {
                        vegetable_id = "100000";
                        ent_main.sort_index = "1";
                    } else {
                        long id1 = Long.parseLong(Common.JSON_GetFieldValue(max_data, "vegetable_id")) + 1;
                        long id2 = Long.parseLong(Common.JSON_GetFieldValue(max_data, "sort_index")) + 1;
                        if (id1 < 100000)
                            id1 = 100000;
                        vegetable_id = String.valueOf(id1);
                        ent_main.sort_index = String.valueOf(id2);
                    }
                    ent_main.vegetable_id = vegetable_id;
                    ac.dao.Insert(ent_main.table_name, ent_main.GetInsertContentValues());
                    aaaa = "7777";
                } else {
                    aaaa = "8888";
                    ac.dao.ExecSQL(ent_main.update_sql, ent_main.GetUpdateValues());
                }
            } else {
                aaaa = "9999";
                ac.dao.ExecSQL(ent_main.update_time_sql, ent_main.GetUpdateTimeValues());
            }
            aaaa = "aaaa";
            //清除旧数据
            t_vegetable_info_steep ent = new t_vegetable_info_steep();
            ent.vegetable_id = vegetable_id;
            ac.dao.ExecSQL(ent.clear_sql, ent.GetClearValues());
            //读取最大的steep_id
            long steep_id = 0;
            Cursor dt_max = ac.dao.GetCursor(ent.max_sql, null);
            int item_count = dt_max.getCount();
            String max_data = ac.dao.Cursor2Json(dt_max);
            dt_max.close();
            if (item_count <= 0)
                steep_id = 1;
            else
                steep_id = Long.parseLong(Common.JSON_GetFieldValue(max_data, "steep_id")) + 1;
            //读取最大的sub_id
            long sub_id = 0;
            t_vegetable_info_steep_vegetable ent1 = new t_vegetable_info_steep_vegetable();
            ent1.vegetable_id = vegetable_id;
            ac.dao.ExecSQL(ent1.clear_sql, ent1.GetClearValues());
            Cursor dt_max1 = ac.dao.GetCursor(ent1.max_sql, null);
            int item_count1 = dt_max.getCount();
            String max_data1 = ac.dao.Cursor2Json(dt_max1);
            dt_max1.close();
            if (item_count1 <= 0)
                sub_id = 1;
            else
                sub_id = Long.parseLong(Common.JSON_GetFieldValue(max_data1, "sub_id")) + 1;
            aaaa = "bbbb";
            //步骤表
            int steep_index = 0;
            JSONObject json_object = new JSONObject(steep_data);
            JSONArray steep_array = json_object.getJSONArray("steep_data");
            //ent_main.steep_qty = String.valueOf(steep_array.length());
            int time_long = 0;
            for (int j = 0; j < steep_array.length(); j++) {
                JSONObject steep_json_object = steep_array.getJSONObject(j);
                t_vegetable_info_steep ent2 = new t_vegetable_info_steep();
                ent2.steep_id = String.valueOf(steep_id++);
                ent2.vegetable_id = vegetable_id;
                ent2.steep_type = steep_json_object.getString("steep_type");
                if (ent2.steep_type == null || ent2.steep_type.length() <= 0)
                    ent2.steep_type = "" + steep_json_object.getInt("steep_type");
                ent2.steep_index = String.valueOf(steep_index++);
                ent2.fire_value = steep_json_object.getString("fire_value");
                ent2.time_value = steep_json_object.getString("time_value");
                ent2.time_long = String.valueOf(Common.time2int(ent2.time_value));
                ent2.speed_value = steep_json_object.getString("speed_value");
                ent2.rotation_value = steep_json_object.getString("rotation_value");
                ent2.nozzle1_value = steep_json_object.getString("nozzle1_value");
                ent2.nozzle2_value = steep_json_object.getString("nozzle2_value");
                ent2.nozzle3_value = steep_json_object.getString("nozzle3_value");
                ent2.tts_text = "";
                ent2.update_time = String.valueOf(Common.GetTimeStamp10());
                if (ent2.steep_type.length() <= 0)
                    ent2.steep_type = "1";
                if (ent2.nozzle1_value.length() <= 0 || ent2.nozzle1_value.equals("null")) ent2.nozzle1_value = "0";
                if (ent2.nozzle2_value.length() <= 0 || ent2.nozzle2_value.equals("null")) ent2.nozzle2_value = "0";
                if (ent2.nozzle3_value.length() <= 0 || ent2.nozzle3_value.equals("null")) ent2.nozzle3_value = "0";
                ac.dao.Insert(ent2.table_name, ent2.GetInsertContentValues());
                time_long += Integer.parseInt(ent2.time_long);
                aaaa = "cccc";
                //配菜数据
                String sub_type_list = "";
                JSONArray vegetable_array = steep_json_object.getJSONArray("vegetable_data");
                for (int k = 0; k < vegetable_array.length(); k++) {
                    JSONObject vegetable_json_object = vegetable_array.getJSONObject(k);
                    t_vegetable_info_steep_vegetable ent3 = new t_vegetable_info_steep_vegetable();
                    ent3.sub_id = String.valueOf(sub_id++);
                    ent3.steep_id = ent2.steep_id;
                    ent3.vegetable_id = ent2.vegetable_id;
                    ent3.sub_type = vegetable_json_object.getString("sub_type");
                    ent3.sub_name = vegetable_json_object.getString("sub_name");
                    ent3.sub_value = vegetable_json_object.getString("sub_value");
                    ent3.update_time = String.valueOf(Common.GetTimeStamp10());
                    sub_type_list += ent3.sub_type + ",";
                    ac.dao.Insert(ent3.table_name, ent3.GetInsertContentValues());
                }
                aaaa = "dddd";

                //合成tts播音文本，steep_type步骤类型：1设备控制、2添加液体料、3添加主配菜
                if (ent2.steep_type.equals("1"))   //1设备控制
                {
                    ent2.tts_text = "自动设备控制";
                }
                if (ent2.steep_type.equals("2"))   //2添加液体料
                {
                    ent2.tts_text = "自动放";
                    if (ent2.nozzle1_value.equals("0") == false)
                        ent2.tts_text += "水" + ent2.nozzle1_value + "克";
                    if (ent2.nozzle2_value.equals("0") == false)
                    {
                        if (ent2.tts_text.endsWith("克"))
                            ent2.tts_text += "、";
                        ent2.tts_text += "油" + ent2.nozzle2_value + "克";
                    }
                    if (ent2.nozzle3_value.equals("0") == false)
                    {
                        if (ent2.tts_text.endsWith("克"))
                            ent2.tts_text += "、";
                        ent2.tts_text += "混合酱料" + ent2.nozzle3_value + "克";
                    }
                }
                if (ent2.steep_type.equals("3"))   //3添加主配菜
                {
                    //sub_type类型：1炒料、2主菜、3配菜、4其他
                    ent2.tts_text = "";
                    if (sub_type_list.contains("1"))
                        ent2.tts_text += "炒料";
                    if (sub_type_list.contains("2"))
                    {
                        if (ent2.tts_text.length() > 0)
                            ent2.tts_text += "、";
                        ent2.tts_text += "主菜";
                    }
                    if (sub_type_list.contains("3"))
                    {
                        if (ent2.tts_text.length() > 0)
                            ent2.tts_text += "、";
                        ent2.tts_text += "配菜";
                    }
                    if (sub_type_list.contains("4"))
                    {
                        if (ent2.tts_text.length() > 0)
                            ent2.tts_text += "、";
                        ent2.tts_text += "其他";
                    }
                    if (ent2.tts_text.length() > 0)
                        ent2.tts_text = "请开始放" + ent2.tts_text;
                }
                if (ent2.tts_text.length() > 0)
                    ac.dao.ExecSQL(ent2.update_ttstext_sql, ent2.GetUpdateTTSValues());
                aaaa = "eeee";

            }
            aaaa = "ffff";
            ent_main.work_time = Common.int2time(time_long);
            ac.dao.ExecSQL(ent_main.update_time_and_qty_sql, ent_main.GetUpdateTimeAndQtyValues());
            aaaa = "gggg";
            ac.dao.ExecSQL(ent_main.update_time_sql, ent_main.GetUpdateTimeValues());

            String return_data = "{\"success\":true,\"desc\":\"保存成功\",\"vegetable_id\":\""+vegetable_id+"\",\"bbbb\":\""+bbbb+"\"}";
            return return_data;
        }
        catch(Exception ex)
        {
            error = ex.getMessage();
        }
        return "{\"success\":false,\"desc\":\"保存失败\",\"aaaa\":\""+aaaa+"\",\"error\":\""+error+"\"}";
    }

    //删除菜谱
    public static String DeleteSQLData_Vegetable(ApplicationClass ac, String vegetable_id)
    {
        try
        {
            t_vegetable_info ent1 = new t_vegetable_info();
            ent1.vegetable_id = vegetable_id;
            ac.dao.ExecSQL(ent1.delete_sql, ent1.GetDeleteValues());
            t_vegetable_info_steep ent2 = new t_vegetable_info_steep();
            ent2.vegetable_id = vegetable_id;
            ac.dao.ExecSQL(ent2.clear_sql, ent2.GetClearValues());
            t_vegetable_info_steep_vegetable ent3 = new t_vegetable_info_steep_vegetable();
            ent3.vegetable_id = vegetable_id;
            ac.dao.ExecSQL(ent3.clear_sql, ent3.GetClearValues());

            String return_data = "{\"success\":true,\"desc\":\"删除成功\",\"vegetable_id\":" + vegetable_id+ "}";
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"删除失败\"}";
    }

    //启动自动清洗，type=1简单清洗、2深度清洗
    public static String GetSQLData_Clean_Start(ApplicationClass ac, String type)
    {
        try
        {
            Cursor dt = null;
            if (type.equals("1")) {
                t_public_config_clean ent1 = new t_public_config_clean();
                dt = ac.dao.GetCursor(ent1.list_sql, null);
            }else{
                t_public_config_clean_depth ent2 = new t_public_config_clean_depth();
                dt = ac.dao.GetCursor(ent2.list_sql, null);
            }
            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = "{\"success\":true,\"desc\":\"成功\",\"item_count\":" + item_count + ",\"item_data\":[" + item_data + "]}";
            dt.close();
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"失败\"}";
    }
    //读取清洗数据，type=1简单清洗、2深度清洗
    public static String GetSQLData_Clean_Entity(ApplicationClass ac, String type)
    {
        try
        {
            Cursor dt = null;
            if (type.equals("1")) {
                t_public_config_clean ent1 = new t_public_config_clean();
                dt = ac.dao.GetCursor(ent1.list_sql, null);
            }else{
                t_public_config_clean_depth ent2 = new t_public_config_clean_depth();
                dt = ac.dao.GetCursor(ent2.list_sql, null);
            }
            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = ",\"clean"+type+"_steep_count\":" + item_count + ",\"clean"+type+"_steep_data\":[" + item_data + "]";
            dt.close();
            return return_data;
        }
        catch(Exception ex)
        {}
        return "";
    }
    //删除清洗步骤，clean_type=1简单清洗、2深度清洗
    public static String DeleteSQLData_Clean_Steep(ApplicationClass ac, String clean_type, String clean_id)
    {
        try
        {
            if (clean_type.equals("1")) {
                t_public_config_clean ent1 = new t_public_config_clean();
                ent1.clean_id = clean_id;
                ac.dao.ExecSQL(ent1.delete_sql, ent1.GetDeleteValues());
            }else {
                t_public_config_clean_depth ent2 = new t_public_config_clean_depth();
                ent2.clean_id = clean_id;
                ac.dao.ExecSQL(ent2.delete_sql, ent2.GetDeleteValues());
            }
            String return_data = "{\"success\":true,\"desc\":\"删除成功\"}";
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"删除失败\"}";
    }
    //保存清洗步骤，clean_type=1简单清洗、2深度清洗
    public static String SaveSQLData_Clean_Steep(ApplicationClass ac, String clean_type, String clean_id, String steep_index, String pot_position, String fire_value, String time_value, String speed_value, String rotation_value, String status)
    {
        try
        {
            if (clean_type.equals("1")) {
                t_public_config_clean ent = new t_public_config_clean();
                ent.clean_id = clean_id;

                //新增
                if (clean_id.equals("0")){
                    Cursor dt = ac.dao.GetCursor(ent.max_sql, null);
                    int item_count = dt.getCount();
                    String max_data = ac.dao.Cursor2Json(dt);
                    dt.close();
                    if (item_count <= 0)
                        ent.clean_id = "1";
                    else
                        ent.clean_id = "" + (Integer.parseInt(Common.JSON_GetFieldValue(max_data, "clean_id")) + 1);
                    clean_id = ent.clean_id;
                    ac.dao.ExecSQL(ent.update_steepindex_sql, ent.GetUpdateSteepIndexValues());
                    ac.dao.Insert(ent.table_name, ent.GetInsertContentValues());
                }else {
                    //编辑
                    ac.dao.ExecSQL(ent.update_sql, ent.GetUpdateValues());
                }
            }else {
                t_public_config_clean_depth ent = new t_public_config_clean_depth();
                ent.clean_id = clean_id;
                ent.steep_index = steep_index;
                ent.pot_position = pot_position;
                ent.fire_value = fire_value;
                ent.time_value = time_value;
                ent.time_long = String.valueOf(Common.time2int(time_value));
                ent.speed_value = speed_value;
                ent.rotation_value = rotation_value;
                ent.status = status;
                ent.tts_text = "";
                ent.update_time = String.valueOf(Common.GetTimeStamp10());
                //新增
                if (clean_id.equals("0")){
                    Cursor dt = ac.dao.GetCursor(ent.max_sql, null);
                    int item_count = dt.getCount();
                    String max_data = ac.dao.Cursor2Json(dt);
                    dt.close();
                    if (item_count <= 0)
                        ent.clean_id = "1";
                    else
                        ent.clean_id = "" + (Integer.parseInt(Common.JSON_GetFieldValue(max_data, "clean_id")) + 1);
                    clean_id = ent.clean_id;
                    ac.dao.ExecSQL(ent.update_steepindex_sql, ent.GetUpdateSteepIndexValues());
                    ac.dao.Insert(ent.table_name, ent.GetInsertContentValues());
                }else {
                    //编辑
                    ac.dao.ExecSQL(ent.update_sql, ent.GetUpdateValues());
                }
            }
            String return_data = "{\"success\":true,\"desc\":\"保存成功\",\"clean_id\":\""+clean_id+"\"}";
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"保存失败\"}";
    }

    //读取一键养锅数据
    public static String GetSQLData_FeedPot_Entity(ApplicationClass ac)
    {
        try
        {
            Cursor dt = null;
            t_public_config_feed_pot ent1 = new t_public_config_feed_pot();
            dt = ac.dao.GetCursor(ent1.list_sql, null);
            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = ",\"steep_count\":" + item_count + ",\"steep_data\":[" + item_data + "]";
            dt.close();
            return return_data;
        }
        catch(Exception ex)
        {}
        return "";
    }
    //删除一键养锅步骤
    public static String DeleteSQLData_FeedPot_Steep(ApplicationClass ac, String clean_id)
    {
        try
        {
            t_public_config_feed_pot ent1 = new t_public_config_feed_pot();
            ent1.clean_id = clean_id;
            ac.dao.ExecSQL(ent1.delete_sql, ent1.GetDeleteValues());
            String return_data = "{\"success\":true,\"desc\":\"删除成功\"}";
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"删除失败\"}";
    }
    //保存一键养锅步骤
    public static String SaveSQLData_FeedPot_Steep(ApplicationClass ac, String clean_id, String steep_index, String pot_position, String fire_value, String time_value, String speed_value, String rotation_value, String status)
    {
        try
        {
            t_public_config_feed_pot ent = new t_public_config_feed_pot();
            ent.clean_id = clean_id;
            ent.steep_index = steep_index;
            ent.pot_position = pot_position;
            ent.fire_value = fire_value;
            ent.time_value = time_value;
            ent.time_long = String.valueOf(Common.time2int(time_value));
            ent.speed_value = speed_value;
            ent.rotation_value = rotation_value;
            ent.status = status;
            ent.tts_text = "";
            ent.update_time = String.valueOf(Common.GetTimeStamp10());
            //新增
            if (clean_id.equals("0")){
                Cursor dt = ac.dao.GetCursor(ent.max_sql, null);
                int item_count = dt.getCount();
                String max_data = ac.dao.Cursor2Json(dt);
                dt.close();
                if (item_count <= 0)
                    ent.clean_id = "1";
                else
                    ent.clean_id = "" + (Integer.parseInt(Common.JSON_GetFieldValue(max_data, "clean_id")) + 1);
                clean_id = ent.clean_id;
                ac.dao.ExecSQL(ent.update_steepindex_sql, ent.GetUpdateSteepIndexValues());
                ac.dao.Insert(ent.table_name, ent.GetInsertContentValues());
            }else {
                //编辑
                ac.dao.ExecSQL(ent.update_sql, ent.GetUpdateValues());
            }

            String return_data = "{\"success\":true,\"desc\":\"保存成功\",\"clean_id\":\""+clean_id+"\"}";
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"保存失败\"}";
    }



    //获取故障记录，start=第n页，默认值为1
    public static String GetSQLData_ErrorInfo(ApplicationClass ac, String search_code, String search_time, String start, String limit)
    {
        try
        {
            //搜索：错误代码
            search_code = search_code.replace("'", "");
            search_code = search_code.replace("%", "");
            //搜索：日期，格式YYYY-MM-DD
            String begin_time = "", end_time = "";
            if (search_time.length() == 10){
                begin_time = String.valueOf(Common.GetTimeStamp13(search_time + " 00:00:00"));
                end_time   = String.valueOf(Common.GetTimeStamp13(search_time + " 23:59:59"));
            }
            //页码，第n页，默认值为1
            if (start == null || start.trim().length() <= 0) start = "1";
            //每页显示n行，默认值为10
            if (limit == null || limit.trim().length() <= 0) limit = "10";

            //转换成sqlite的页码，从0开始
            int page_index = Integer.parseInt(start) - 1;
            int page_limit = Integer.parseInt(limit);

            String sql_select = "select * ";
            String sql_where = " where timestamp>0 ";
            if (search_code.length() > 0)
                sql_where += " and error_code like '%"+search_code+"%' ";
            if (begin_time.length() > 0 )
                sql_where += " and timestamp>=" + begin_time + " ";
            if (end_time.length() > 0 )
                sql_where += " and timestamp<=" + end_time + " ";

            //求总记录数
            String sql_count = "select count(*) as qty from t_error_record " + sql_where;
            Cursor dt_count = ac.dao.GetCursor(sql_count, null);
            String json_count = ac.dao.Cursor2Json(dt_count);
            dt_count.close();
            int record_count = Integer.parseInt(Common.JSON_GetFieldValue(json_count, "qty"));
            int page_count = record_count / page_limit;
            if (record_count % page_limit > 0)
                page_count++;

            //读取分页
            int offset = (page_index - 1) * page_limit;
            String sql_page = sql_select + " from t_error_record " + sql_where + " order by timestamp desc limit " + offset + "," + page_limit;
            Cursor dt_pay = ac.dao.GetCursor(sql_page, null);
            int data_size = dt_pay.getCount();
            String data = ac.dao.Cursor2Json(dt_pay);
            dt_pay.close();

            String return_data = "{\"success\": true,\"page_index\":"+start+",\"page_count\":"+page_count+",\"limit\":"+limit+",\"record_count\":"+record_count+",\"data_size\":"+data_size+",\"data\":["+data+"]}";
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //清除故障记录
    public static String DeleteSQLData_ErrorInfo(ApplicationClass ac)
    {
        try
        {
            t_error_record ent = new t_error_record();
            ac.dao.ExecSQL(ent.delete_sql, ent.GetDeleteValues());

            String return_data = "{\"success\":true,\"desc\":\"成功\"}";
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"失败\"}";
    }


    //启动一键养锅
    public static String GetSQLData_FeedPot_Start(ApplicationClass ac)
    {
        try
        {
            Cursor dt = null;

            t_public_config_feed_pot ent2 = new t_public_config_feed_pot();
            dt = ac.dao.GetCursor(ent2.list_sql, null);

            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = "{\"success\":true,\"desc\":\"成功\",\"item_count\":" + item_count + ",\"item_data\":[" + item_data + "]}";
            dt.close();
            return return_data;
        }
        catch(Exception ex)
        {}
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //手动炒制--读取7处组合列表
    //type=1手动炒制、2自动录制
    public static String GetSQLData_Handing_Config_List(ApplicationClass ac, int type) {
        try {
            Cursor dt;
            if (type == 1) {
                t_handing_config_nozzle ent = new t_handing_config_nozzle();
                dt = ac.dao.GetCursor(ent.list_sql, null);
            } else {
                t_handing_config_nozzle2 ent = new t_handing_config_nozzle2();
                dt = ac.dao.GetCursor(ent.list_sql, null);
            }

            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = "{\"success\":true,\"desc\":\"成功\",\"item_count\":" + item_count + ",\"item_data\":[" + item_data + "]}";
            dt.close();
            return return_data;
        }
        catch (Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_Handing_Config_List()出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //手动炒制--读取7处组合列表（格式化后的）
    //type=1手动炒制、2自动录制
    public static String GetSQLData_Handing_Config_Format(ApplicationClass ac, int type) {
        try {
            Cursor dt;
            if (type == 1) {
                t_handing_config_nozzle ent = new t_handing_config_nozzle();
                dt = ac.dao.GetCursor(ent.list_sql, null);
            } else {
                t_handing_config_nozzle2 ent = new t_handing_config_nozzle2();
                dt = ac.dao.GetCursor(ent.list_sql, null);
            }
            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = "[" + item_data + "]";
            dt.close();



            item_data = "";
            JSONArray list = new JSONArray(return_data);
            for (int i=0; i<list.length(); i++) {
                JSONObject obj = list.getJSONObject(i);
                String sub_str = HandingConfig2String(ac, obj);
                if (item_data.length() > 0 && sub_str.length() > 0)
                    item_data += ",";
                item_data += sub_str;
            }

            return_data = "{\"success\":true,\"desc\":\"成功\",\"item_count\":" + item_count + ",\"item_data\":[" + item_data + "]}";
            return return_data;
        }
        catch (Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_Handing_Config_Format()出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }
    private static String HandingConfig2String(ApplicationClass ac, JSONObject obj) {
        try {
            int config_id = obj.getInt("config_id");
            int nozzle1_value = obj.getInt("nozzle1_value");
            int nozzle2_value = obj.getInt("nozzle2_value");
            int nozzle3_value = obj.getInt("nozzle3_value");
            int nozzle4_value = obj.getInt("nozzle4_value");
            int nozzle5_value = obj.getInt("nozzle5_value");
            int nozzle6_value = obj.getInt("nozzle6_value");
            int nozzle7_value = obj.getInt("nozzle7_value");
            int nozzle8_value = obj.getInt("nozzle8_value");
            int nozzle9_value = obj.getInt("nozzle9_value");
            int nozzle10_value = obj.getInt("nozzle10_value");
            int nozzle11_value = obj.getInt("nozzle11_value");
            String name = "", text = "";
            if (config_id <= 3) {
                if (nozzle1_value > 0) { name = Common.nozzle_name_list[1]; text = nozzle1_value + "g"; }
                if (nozzle2_value > 0) { name = Common.nozzle_name_list[2]; text = nozzle2_value + "g"; }
                if (nozzle3_value > 0) { name = Common.nozzle_name_list[3]; text = nozzle3_value + "g"; }
                if (nozzle4_value > 0) { name = Common.nozzle_name_list[4]; text = nozzle4_value + "g"; }
                if (nozzle5_value > 0) { name = Common.nozzle_name_list[5]; text = nozzle5_value + "g"; }
                if (nozzle6_value > 0) { name = Common.nozzle_name_list[6]; text = nozzle6_value + "g"; }
                if (nozzle7_value > 0) { name = Common.nozzle_name_list[7]; text = nozzle7_value + "g"; }
                if (nozzle8_value > 0) { name = Common.nozzle_name_list[8]; text = nozzle8_value + "g"; }
                if (nozzle9_value > 0) { name = Common.nozzle_name_list[9]; text = nozzle9_value + "g"; }
                if (nozzle10_value > 0) { name = Common.nozzle_name_list[10]; text = nozzle10_value + "g"; }
                if (nozzle11_value > 0) { name = Common.nozzle_name_list[11]; text = nozzle11_value + "g"; }
                if (name.length() == 0) {
                    name = Common.nozzle_name_list[config_id];
                    text = "0g";
                }
            } else {
                name = "组合" + (config_id - 3);
                if (nozzle1_value > 0) { text += "/" + Common.nozzle_name_list[1] + "------" + nozzle1_value + "g"; }
                if (nozzle2_value > 0) { text += "/" + Common.nozzle_name_list[2] + "------" + nozzle2_value + "g"; }
                if (nozzle3_value > 0) { text += "/" + Common.nozzle_name_list[3] + "------" + nozzle3_value + "g"; }
                if (nozzle4_value > 0) { text += "/" + Common.nozzle_name_list[4] + "------" + nozzle4_value + "g"; }
                if (nozzle5_value > 0) { text += "/" + Common.nozzle_name_list[5] + "------" + nozzle5_value + "g"; }
                if (nozzle6_value > 0) { text += "/" + Common.nozzle_name_list[6] + "------" + nozzle6_value + "g"; }
                if (nozzle7_value > 0) { text += "/" + Common.nozzle_name_list[7] + "------" + nozzle7_value + "g"; }
                if (nozzle8_value > 0) { text += "/" + Common.nozzle_name_list[8] + "------" + nozzle8_value + "g"; }
                if (nozzle9_value > 0) { text += "/" + Common.nozzle_name_list[9] + "------" + nozzle9_value + "g"; }
                if (nozzle10_value > 0) { text += "/" + Common.nozzle_name_list[10] + "------" + nozzle10_value + "g"; }
                if (nozzle11_value > 0) { text += "/" + Common.nozzle_name_list[11] + "------" + nozzle11_value + "g"; }
                if (text.length() == 0) text = "未配置";
                if (text.startsWith("/")) text = text.substring(1);
            }
            return "{\"config_id\":"+config_id+",\"name\":\""+name+"\",\"text\":\""+text+"\"}";
        }catch (Exception ex) {
            ac.SaveLog("WebUtil.HandingConfig2String()出错：" + ex.getMessage());
        }
        return "";
    }

    //手动炒制--读取料盒配置
    //type=1手动炒制、2自动录制
    public static String GetSQLData_Handing_Config_Box(ApplicationClass ac, int type) {
        try {
            Cursor dt;
            if (type == 1) {
                t_handing_config_box ent = new t_handing_config_box();
                dt = ac.dao.GetCursor(ent.list_sql, ent.GetListValues());
            } else {
                t_handing_config_box2 ent = new t_handing_config_box2();
                dt = ac.dao.GetCursor(ent.list_sql, ent.GetListValues());
            }
            String item_count = String.valueOf(dt.getCount());
            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = "{\"success\":true,\"desc\":\"成功\",\"data\":[" + item_data + "]}";
            dt.close();
            return return_data;
        }
        catch (Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_Handing_Config_Box()出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //手动炒制--保存料盒配置
    //type=1手动炒制、2自动录制
    public static void SaveSQLData_Handing_Config_Box(ApplicationClass ac, int box1_value, int box2_value, int box3_value, int box4_value, int type)
    {
        try
        {
            if (type == 1) {
                t_handing_config_box ent = new t_handing_config_box();
                ent.box1_value = "" + box1_value;
                ent.box2_value = "" + box2_value;
                ent.box3_value = "" + box3_value;
                ent.box4_value = "" + box4_value;
                ac.dao.ExecSQL(ent.update_sql, ent.GetUpdateValues());
            } else {
                t_handing_config_box2 ent = new t_handing_config_box2();
                ent.box1_value = "" + box1_value;
                ent.box2_value = "" + box2_value;
                ent.box3_value = "" + box3_value;
                ent.box4_value = "" + box4_value;
                ac.dao.ExecSQL(ent.update_sql, ent.GetUpdateValues());
            }
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.SaveSQLData_Handing_Config_Box("+box1_value+","+box2_value+","+box3_value+","+box4_value+")出错：" + ex.getMessage());
        }
    }

    //读取喷泵组合配置
    //type=1手动炒制、2自动录制
    public static String GetSQLData_Handing_Config_Nozzle_Entity(ApplicationClass ac, int config_id, int type) {
        try
        {
            Cursor dt;
            if (type == 1) {
                t_handing_config_nozzle ent = new t_handing_config_nozzle();
                ent.config_id = "" + config_id;
                dt = ac.dao.GetCursor(ent.entity_sql, ent.GetEntityValues());
            } else {
                t_handing_config_nozzle2 ent = new t_handing_config_nozzle2();
                ent.config_id = "" + config_id;
                dt = ac.dao.GetCursor(ent.entity_sql, ent.GetEntityValues());
            }

            String item_data = ac.dao.Cursor2Json(dt);
            String return_data = "{\"success\":true,\"desc\":\"成功\",\"data\":" + item_data + "}";
            dt.close();

            return return_data;
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_Handing_Config_Nozzle_Entity("+config_id+")出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //保存喷泵组合配置
    //type=1手动炒制、2自动录制
    public static boolean SaveSQLData_Handing_Config_Nozzle_Entity(ApplicationClass ac, int config_id, int nozzle1_value, int nozzle2_value, int nozzle3_value, int nozzle4_value, int nozzle5_value, int nozzle6_value, int nozzle7_value, int nozzle8_value, int nozzle9_value, int nozzle10_value, int nozzle11_value, int type) {
        try
        {
            if (type == 1) {
                t_handing_config_nozzle ent = new t_handing_config_nozzle();
                ent.config_id = "" + config_id;
                ent.nozzle1_value = "" + nozzle1_value;
                ent.nozzle2_value = "" + nozzle2_value;
                ent.nozzle3_value = "" + nozzle3_value;
                ent.nozzle4_value = "" + nozzle4_value;
                ent.nozzle5_value = "" + nozzle5_value;
                ent.nozzle6_value = "" + nozzle6_value;
                ent.nozzle7_value = "" + nozzle7_value;
                ent.nozzle8_value = "" + nozzle8_value;
                ent.nozzle9_value = "" + nozzle9_value;
                ent.nozzle10_value = "" + nozzle10_value;
                ent.nozzle11_value = "" + nozzle11_value;
                ac.dao.ExecSQL(ent.update_sql, ent.GetUpdateValues());
            } else {
                t_handing_config_nozzle2 ent = new t_handing_config_nozzle2();
                ent.config_id = "" + config_id;
                ent.nozzle1_value = "" + nozzle1_value;
                ent.nozzle2_value = "" + nozzle2_value;
                ent.nozzle3_value = "" + nozzle3_value;
                ent.nozzle4_value = "" + nozzle4_value;
                ent.nozzle5_value = "" + nozzle5_value;
                ent.nozzle6_value = "" + nozzle6_value;
                ent.nozzle7_value = "" + nozzle7_value;
                ent.nozzle8_value = "" + nozzle8_value;
                ent.nozzle9_value = "" + nozzle9_value;
                ent.nozzle10_value = "" + nozzle10_value;
                ent.nozzle11_value = "" + nozzle11_value;
                ac.dao.ExecSQL(ent.update_sql, ent.GetUpdateValues());
            }
            return true;
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.SaveSQLData_Handing_Config_Nozzle_Entity("+config_id+")出错：" + ex.getMessage());
        }
        return false;
    }

    //读取新的菜谱名称
    public static String GetSQLData_Cooking_Config_NewName(ApplicationClass ac, int vegetable_id) {
        try
        {
            String json_data = GetSQLData_Vegetable_Entity(ac, "" + vegetable_id);
            JSONObject json_obj = new JSONObject(json_data);
            String vegetable_name = json_obj.getJSONObject("data").getString("vegetable_name");
            int n = vegetable_name.indexOf("副本");
            if (n >= 0)
                vegetable_name = vegetable_name.substring(0, n);

            for (int i=1; i<=200; i++) {
                String vegetable_name_new = vegetable_name + i;
                json_data = GetSQLData_Vegetable_IsExists(ac, "0", vegetable_name_new);
                if (json_data.contains(":0}"))
                    return vegetable_name_new;
            }
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_Cooking_Config_NewName("+vegetable_id+")出错：" + ex.getMessage());
        }
        return "副本1";
    }

    //复制一个菜+规格，生成另一个新菜
    public static String SaveSQLData_Cooking_Config_CopyNew(ApplicationClass ac, int vegetable_id, int spec_id, String vegetable_name) {
        try
        {
            //读取系统菜谱详情
            t_vegetable_info tb = new t_vegetable_info();
            tb.vegetable_id = "" + vegetable_id;
            String json_data = WebUtil.GetSQLData_Vegetable_Entity(ac, tb.vegetable_id);
            JSONObject json_object = new JSONObject(json_data);
            JSONObject data_object = json_object.getJSONObject("data");

            //读取全部规格
            JSONArray spec_data = data_object.getJSONArray("spec_data");
            JSONObject spec_object = null;
            for (int i=0; i<spec_data.length(); i++){
                if (spec_data.getJSONObject(i).getString("spec_id").equals("" + spec_id)) {
                    spec_object = spec_data.getJSONObject(i);
                    break;
                }
            }
            if (spec_object == null) {
                ac.SaveLog("WebUtil.SaveSQLData_Cooking_Config_CopyNew("+vegetable_id+","+spec_id+")规格id不正确");
                return "{\"success\":false,\"desc\":\"规格id不正确\"}";
            }
            JSONArray spec_data_new = new JSONArray();
            spec_data_new.put(spec_object);
            data_object.putOpt("spec_data", spec_data_new);
            spec_data = spec_data_new;

            //获取最大的一个菜谱id
            Cursor dt_main_max = ac.dao.GetCursor(tb.max_sql, null);
            int item_count = dt_main_max.getCount();
            String max_data = ac.dao.Cursor2Json(dt_main_max);
            dt_main_max.close();
            if (item_count <= 0) {
                tb.vegetable_id = "100000";
                tb.sort_index = "1";
            } else {
                long id1 = Long.parseLong(Common.JSON_GetFieldValue(max_data, "vegetable_id")) + 1;
                if (id1 < 100000)
                    id1 = 100000;
                tb.vegetable_id = String.valueOf(id1);
            }
            //填充菜谱内容
            tb.company_id = ac.company_id;
            tb.parent_company_id = ac.parent_company_id;
            tb.vegetable_name = vegetable_name;
            tb.work_time = data_object.getString("work_time");
            tb.sort_index = data_object.getString("sort_index");
            tb.push_src = "2";  //2微调过的
            tb.face_image_url = data_object.getString("face_image_url");
            tb.spec_data = spec_data.toString();
            tb.firsh_char = Common.Chinese_getFirstLetter(vegetable_name).substring(0, 1).toUpperCase();
            tb.from_vegetable_id = "" + vegetable_id;
            tb.last_work_time = "0";
            tb.user_work_qty = "0";
            tb.update_time = String.valueOf(Common.GetTimeStamp10());
            ac.dao.Insert(tb.table_name, tb.GetInsertContentValues());
            return "{\"success\":true,\"vegetable_id\":"+tb.vegetable_id+",\"spec_id\":"+spec_id+"}";
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.SaveSQLData_Cooking_Config_CopyNew("+vegetable_id+","+spec_id+","+vegetable_name+")出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //保存一个新菜（录制，要判断菜名存在了但份数不一样）
    public static String SaveSQLData_Handing_New(ApplicationClass ac, JSONObject json_object) {
        try
        {
            //菜谱表
            t_vegetable_info tb = new t_vegetable_info();
            //读取规格
            JSONArray spec_data = json_object.getJSONArray("spec_data");
            //获取最大的一个菜谱id
            Cursor dt_main_max = ac.dao.GetCursor(tb.max_sql, null);
            int item_count = dt_main_max.getCount();
            String max_data = ac.dao.Cursor2Json(dt_main_max);
            dt_main_max.close();
            if (item_count <= 0) {
                tb.vegetable_id = "10000000";
                tb.sort_index = "1";
            } else {
                long id1 = Long.parseLong(Common.JSON_GetFieldValue(max_data, "vegetable_id")) + 1;
                if (id1 < 10000000)
                    id1 = 10000000;
                tb.vegetable_id = String.valueOf(id1);
            }
            //填充菜谱内容
            tb.company_id = ac.company_id;
            tb.parent_company_id = ac.parent_company_id;
            tb.vegetable_name = json_object.getString("vegetable_name");
            tb.work_time = json_object.getString("work_time");
            tb.sort_index = "0";
            tb.push_src = "3";  //3本地录制或新增的
            tb.face_image_url = json_object.getString("face_image_url");
            tb.spec_data = spec_data.toString();
            tb.firsh_char = Common.Chinese_getFirstLetter(tb.vegetable_name).substring(0, 1).toUpperCase();
            tb.from_vegetable_id = "0";
            tb.last_work_time = "0";
            tb.user_work_qty = "0";
            tb.update_time = String.valueOf(Common.GetTimeStamp10());
            tb.server_vegetable_id = "0";
            tb.is_need_sendtoserver = "1";
            ac.dao.Insert(tb.table_name, tb.GetInsertContentValues());
            System.out.print("插表vegetable_id="+tb.vegetable_id+",spec_data=" + tb.spec_data);
            //这里加一个保存到云端同步菜谱  2025.06.20
            SendVegetableToServer(ac, tb);
            return "{\"success\":true,\"vegetable_id\":"+tb.vegetable_id+",\"spec_id\":"+json_object.getJSONArray("spec_data").getJSONObject(0).getString("spec_id")+"}";
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.SaveSQLData_Handing_New("+json_object.toString()+")出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }
    //保存一个旧菜
    public static String SaveSQLData_Handing_Old(ApplicationClass ac, int vegetable_id, JSONObject json_object) {
        try
        {
            //菜谱表
            t_vegetable_info tb = new t_vegetable_info();
            tb.vegetable_id = "" + vegetable_id;
            //读取规格
            JSONArray spec_data = json_object.getJSONArray("spec_data");
            for (int i=0; i<spec_data.length(); i++) {
                spec_data.getJSONObject(i).put("spec_id", (i+1));
            }
            //填充菜谱内容
            tb.face_image_url = json_object.getString("face_image_url");
            tb.spec_data = spec_data.toString();
            tb.update_time = "" + Common.GetTimeStamp10();
            tb.is_need_sendtoserver = "1";
            ac.dao.ExecSQL(tb.update_spec_sql3, tb.GetUpdateSpecValues3());
            //这里加一个保存到云端同步菜谱  2025.06.20
            tb.company_id = ac.company_id;
            tb.parent_company_id = ac.parent_company_id;
            tb.vegetable_name = json_object.getString("vegetable_name");
            tb.work_time = json_object.getString("work_time");
            tb.sort_index = json_object.has("sort_index") ? json_object.getString("sort_index") : "0";
            tb.push_src = json_object.has("push_src") ? json_object.getString("push_src") : "0";
            tb.firsh_char = json_object.has("firsh_char") ? json_object.getString("firsh_char") : "";
            tb.from_vegetable_id = json_object.has("from_vegetable_id") ? json_object.getString("from_vegetable_id") : "0";
            tb.last_work_time = "0";
            tb.user_work_qty = "0";
            tb.server_vegetable_id = json_object.has("server_vegetable_id") ? json_object.getString("server_vegetable_id") : "0";
            SendVegetableToServer(ac, tb);

            return "{\"success\":true,\"vegetable_id\":"+tb.vegetable_id+",\"spec_id\":"+json_object.getJSONArray("spec_data").getJSONObject(0).getString("spec_id")+"}";
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.SaveSQLData_Handing_Old("+vegetable_id+")出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //获取调味料校准列表
    public static String GetSQLData_Calibration_List(ApplicationClass ac) {
        try
        {
            t_check_nozzle ent = new t_check_nozzle();
            Cursor dt = ac.dao.GetCursor(ent.list_sql, ent.GetListValues());
            String item_data = ac.dao.Cursor2Json(dt);
            JSONArray json_array = new JSONArray("[" + item_data + "]");
            item_data = "";
            long time2 = Common.GetTimeStamp10(Common.GetDataTimeString().substring(0, 10).trim() + " 00:00:00");
            for (int i=1; i<Common.nozzle_name_list.length; i++){
                if (Common.nozzle_name_list[i].length() > 0) {
                    JSONObject sub_obj = json_array.getJSONObject(i - 1);
                    int is_need_calibration = 0;
                    long time1 = sub_obj.getLong("last_time");
                    if (time2 - time1 >= 3600 * 24 * 15)
                        is_need_calibration = 1;
                    String sub_item = "{\"nozzle_id\":" + sub_obj.getString("nozzle_id")
                            + ",\"nozzle_name\":\"" + Common.nozzle_name_list[i] + "\""
                            + ",\"is_need_calibration\":" + is_need_calibration + "}";
                    if (item_data.length() > 0)
                        item_data += ",";
                    item_data += sub_item;
                }
            }

            String return_data = "{\"success\":true,\"desc\":\"成功\",\"data\":[" + item_data + "]}";
            dt.close();

            return return_data;
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_Calibration_List()出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //获取调味料放置列表
    public static String GetSQLData_Nozzle_List(ApplicationClass ac) {
        try
        {
            t_check_nozzle ent = new t_check_nozzle();
            Cursor dt = ac.dao.GetCursor(ent.list_sql, null);
            String item_data = ac.dao.Cursor2Json(dt);
            JSONArray json_array = new JSONArray("[" + item_data + "]");
            item_data = "";
            for (int i=1; i<Common.nozzle_name_list.length; i++){
                if (Common.nozzle_name_list[i].length() > 0) {
                    JSONObject sub_obj = json_array.getJSONObject(i - 1);
                    String sub_item = "{\"title1\":\"喷嘴" + sub_obj.getString("nozzle_id") + "\""
                            + ",\"title2\":\"" + Common.nozzle_name_list[i] + "\"}";
                    if (item_data.length() > 0)
                        item_data += ",";
                    item_data += sub_item;
                }
            }

            String return_data = "{\"success\":true,\"desc\":\"成功\",\"data\":[" + item_data + "]}";
            dt.close();

            return return_data;
        }
        catch(Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_Nozzle_List()出错：" + ex.getMessage());
        }
        return "{\"success\":false,\"desc\":\"失败\"}";
    }

    //获取要清洗的料管名称
    public static String GetSQLData_Cleaning_Nozzle_GetNameList(ApplicationClass ac)
    {
        String name_list = "";
        try {
            //料管清洗配置表
            t_public_config_clean_nozzle ent = new t_public_config_clean_nozzle();
            long time2 = Common.GetTimeStamp10(Common.GetDataTimeString().substring(0, 11).trim() + " 00:00:00");
            Cursor dt = ac.dao.GetCursor(ent.list_sql, null);
            String item_data = ac.dao.Cursor2Json(dt);
            JSONObject json_object = new JSONObject(item_data);
            for (int i=1; i<=11; i++) {
                int is_nozzle = Integer.parseInt(json_object.getString("is_nozzle" + i));
                int value = Integer.parseInt(json_object.getString("nozzle"+i+"_value"));
                int time1 = Integer.parseInt(json_object.getString("nozzle"+i+"_time"));
                if (is_nozzle == 1 && time2 - time1 >= 3600 * 24 * value) {
                    if (name_list.length() > 0)
                        name_list += "、";
                    name_list += "[" + Common.nozzle_name_list[i] + "]";
                }
            }
        }
        catch (Exception ex)
        {
            ac.SaveLog("WebUtil.GetSQLData_Cleaning_Nozzle_GetNameList()出错：" + ex.getMessage());
        }

        return name_list;
    }

    //发送菜谱同步到云端保存
    public static void SendVegetableToServer(ApplicationClass ac, t_vegetable_info tb) {
        try {
            String json_data = "{\"vegetable_id\":" + tb.vegetable_id + ""
                    + ",\"company_id\":" + tb.company_id + ""
                    + ",\"parent_company_id\":" + tb.parent_company_id + ""
                    + ",\"vegetable_name\":\"" + tb.vegetable_name.replace("\"", "") + "\""
                    + ",\"work_time\":\"" + tb.work_time + "\""
                    + ",\"sort_index\":" + tb.sort_index + ""
                    + ",\"push_src\":" + tb.push_src + ""
                    + ",\"face_image_url\":\"" + tb.face_image_url + "\""
                    + ",\"spec_data\":" + tb.spec_data + ""
                    + ",\"firsh_char\":\"" + tb.firsh_char + "\""
                    + ",\"from_vegetable_id\":" + tb.from_vegetable_id + ""
                    + ",\"last_work_time\":" + tb.last_work_time + ""
                    + ",\"user_work_qty\":" + tb.user_work_qty + ""
                    + ",\"update_time\":" + tb.update_time + ""
                    + ",\"server_vegetable_id\":" + tb.server_vegetable_id + ""
                    + ",\"token\":\"" + ac.token + "\"}";

            String post_data = "post_data=" + URLEncoder.encode(json_data);
            String api_url = "?txt_type=/potnew/put_vegetable/&save_type=sendvegetabletoserver&device_id=" + ac.activity_device_id;
            ac.HttpPost(api_url, post_data, 0);
        } catch (Exception ex) {

        }
    }



}
